package com.sixteengroup.personnelms.controller.mesadmin;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.github.pagehelper.PageInfo;
import com.sixteengroup.personnelms.bean.Article;
import com.sixteengroup.personnelms.bean.ComInfo;
import com.sixteengroup.personnelms.bean.Recruit;
import com.sixteengroup.personnelms.service.mesadmin.MessageService;
import com.sixteengroup.personnelms.util.JWTutil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 月下赶路人
 * @Date 2021-08-13 18:28
 * @function 公司信息管理查询__Controller
 */
@RestController
@JsonIgnoreProperties(value = {"handler"})
public class MessageController {

    @Autowired
    MessageService messageService;

    @GetMapping("/comInfoMeg/getComInfo")
    public Map<String, Object> getComInfo() {
        Map<String, Object> map = new HashMap<>();
        try {
            ComInfo comInfo = messageService.getComInfo();
            map.put("comInfo", comInfo);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    @PostMapping("/comInfoMeg/saveComInfo")
    public Map<String, Object> saveComInfo(@RequestBody ComInfo comInfo) {
        Map<String, Object> map = new HashMap<>();
        try {
            messageService.saveComInfo(comInfo);
            map.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
        }
        return map;
    }

    //门户网站获取文章列表
    @PostMapping("/articleMeg/getArticle")
    public Map<String, Object> getArticle(@RequestBody Article queryInfo) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageInfo<Article> pageInfo = messageService.getArticle(queryInfo);
            map.put("articles", pageInfo.getList());
            map.put("total", pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //根据id获取文章信息
    @GetMapping(path = "/articleMeg/getArticleInfo")
    public Map<String, Object> getArticleInfo(String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Article articleInfo = messageService.getArticleInfo(id);
            map.put("articleInfo", articleInfo);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }


    @PostMapping("/articleMeg/saveArticle")
    public Map<String, Object> saveArticle(@RequestBody Article article,@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer operId = JWTutil.getTokenInfo(token).getClaim("id").asInt();
            article.setOperId(operId);
            article.setOperTime(new Date());
            messageService.saveArticle(article);
            map.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //删除用户
    @DeleteMapping(path = "/articleMeg/deleteArticle/{id}")
    public Map<String, Object> deleteArticle(@PathVariable("id") String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            messageService.deleteArticle(id);
            map.put("code", 200);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //编辑保存文章信息
    @PostMapping("/articleMeg/updateArticle")
    public Map<String, Object> updateArticle(@RequestBody Article article,@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer operId = JWTutil.getTokenInfo(token).getClaim("id").asInt();
            article.setOperId(operId);
            article.setOperTime(new Date());
            messageService.updateArticle(article);
            map.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //门户网站获取招聘信息列表
    @PostMapping("/recruitMeg/getRecruit")
    public Map<String, Object> getRecruit(@RequestBody Recruit queryInfo) {
        Map<String, Object> map = new HashMap<>();
        try {
            PageInfo<Recruit> pageInfo = messageService.getRecruit(queryInfo);
            map.put("recruits", pageInfo.getList());
            map.put("total", pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }


    //根据id获取完整招聘信息
    @GetMapping(path = "/recruitMeg/getRecruitInfo")
    public Map<String, Object> getRecruitInfo(String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            Recruit recruitInfo = messageService.getRecruitInfo(id);
            map.put("recruitInfo", recruitInfo);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //保存新的招聘信息
    @PostMapping("/recruitMeg/saveRecruitInfo")
    public Map<String, Object> saveRecruitInfo(@RequestBody Recruit recruit) {
        Map<String, Object> map = new HashMap<>();
        try {
            recruit.setPubdate(new Date());
            messageService.saveRecruitInfo(recruit);
            map.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //删除招聘信息
    @DeleteMapping(path = "/recruitMeg/deleteRecruit/{id}")
    public Map<String, Object> deleteRecruit(@PathVariable("id") String id) {
        Map<String, Object> map = new HashMap<>();
        try {
            messageService.deleteRecruit(id);
            map.put("code", 200);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //编辑保存招聘信息
    @PostMapping("/recruitMeg/updateRecruit/")
    public Map<String, Object> updateRecruit(@RequestBody Recruit recruit) {
        Map<String, Object> map = new HashMap<>();
        try {
            recruit.setPubdate(new Date());
            messageService.updateRecruit(recruit);
            map.put("code", 200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //投递简历
    @PostMapping("/recruitMeg/sendRecruit/{deptName}")
    public Map<String, Object> sendRecruit(@PathVariable("deptName") String deptName,@RequestHeader("token") String token) {
        Map<String, Object> map = new HashMap<>();
        try {
            Integer id = JWTutil.getTokenInfo(token).getClaim("id").asInt();
            String flag = messageService.judge(id);//判断是否进行过多次投递
            if (flag == null) {
                messageService.sendRecruit(deptName,id);
                map.put("code", 200);
            }else{
                map.put("code", 201);//您已投递,请勿重复投递
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }

    //根据id获取完整招聘信息
    @GetMapping(path = "/recruitMeg/getDeptName")
    public Map<String, Object> getDeptName() {
        Map<String, Object> map = new HashMap<>();
        try {
            List<String> deptName = messageService.getDeptName();
            map.put("deptName", deptName);
        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "系统错误");
        }
        return map;
    }


}
